package com.shg.web.ztest;

import java.util.List;
import java.util.UUID;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.shg.bean.Book;
import com.shg.bean.BookType;
import com.shg.bean.Online;
import com.shg.bean.Shop;
import com.shg.service.BookService;

public class BookServiceTest {

	private static BookService bookService;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ApplicationContext context=new ClassPathXmlApplicationContext("application-web-context.xml");
		bookService = (BookService)context.getBean("bookServiceBean");
	}

	@Test
	public void insert(){
		Book book=new Book();
		book.setId(UUID.randomUUID().toString());
		book.setBookName("XXXXX");
		BookType parent=new BookType();
		parent.setId("4028897135d1516c0135d15171e00000");
		book.setParent(parent);
		book.setPage(230);
		book.setPrice(23);
		book.setType(1);
		Shop shop=new Shop();
		shop.setId("ff80808135a033220135a033246c0000");
		book.setShop(shop);
		bookService.insert(book);
	}
	
	@Test
	public void testGetBook(){
		Book book=null;
		try {
			book = bookService.getBook("297ec07635ffe5b00135ffe782750000");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(book.getBookName());
		System.out.println(book.getShop().getShopName());
		for(Online online:book.getShop().getOnlines()){
			if(online.getUserType()==1){
				System.out.println(online.getUserName());
			}else if(online.getUserType()==2){
				System.out.println(online.getUserName());
			}else{
				System.out.println(online.getUserName());
			}
		}
	}
	
	@Test
	public void getDate(){
		List<Book> books=null;
		try {
			books = bookService.getPagination(Book.class, 0, 10).getResultList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(Book book:books){
			System.out.println(book.getBookName());
		}
	}
	@Test
	public void testGetNewBook(){
		try {
			bookService.getNewBook("ff80808135a033220135a033246c0000", 0, 10);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
